package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type DirtyWordsRequest struct {
	DirtyWordsList []string `json:"DirtyWordsList"` // 需要新增的dirty words
}

type DirtyWordsResponse struct {
	ActionStatus string `json:"ActionStatus"`
	ErrorInfo    string `json:"ErrorInfo"`
	ErrorCode    int    `json:"ErrorCode"`
}

/*
向腾讯增加dirty words列表
脏字长度不得超过50个字
*/
func (s sdk) DirtyWordsAdd(req DirtyWordsRequest) bool {
	reqUrl := s.BuildRestfulUrl("v4/openim_dirty_words/add")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("DirtyWordsAdd error: ", err)
		return false
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	resp := ForbidSendMsgResponse{}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("DirtyWordsAdd Unmarshal error: ", err)
		return false
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("DirtyWordsAdd  error: ", err, "error message : ", resp.ErrorInfo)
		return false
	}
	return true
}
